package com.honghe.handler;

import com.honghe.constant.MessageConstant;
import com.honghe.exception.BaseException;
import com.honghe.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;


@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler
    public Result exceptionHandler(BaseException e){
        log.info("异常信息:{}",e.getMessage());
        return Result.error(e.getMessage());
    }
    @ExceptionHandler
    public Result exceptionHandler(SQLIntegrityConstraintViolationException throwables){
        String message = throwables.getMessage();
        if (message.contains("Duplicate entry")){
            String[] split = message.split(" ");
            String s = split[2];
            return Result.error(s+"手机号已存在");
        }
        return Result.error(MessageConstant.UNKNOWN_ERROR);
    }
}
